package com.system.boot.company.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.system.api.SuperInterface;
import com.system.api.company.entity.dto.AssignUserDto;
import com.system.api.company.entity.dto.CompanyFormDto;
import com.system.api.company.entity.po.SysCompany;
import com.system.api.company.entity.search.CompanySearch;
import com.system.api.setting.entity.po.SysDict;
import com.system.boot.company.converter.CompanyConverter;
import com.system.boot.company.service.CompanyService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.yang.common.core.exception.BaseException;
import org.yang.common.core.model.UserInfo;
import org.yang.common.core.result.BaseController;
import org.yang.common.core.result.R;
import org.yang.common.utils.jwt.JwtTokenAnalysisUtli;
import org.yang.database.orm.entity.BaseEnabled;
import org.yang.database.orm.entity.BaseQuery;
import org.yang.database.orm.entity.Options;

import java.util.List;
import java.util.Objects;


/**
 * 企业管理
 * @author: lslands
 * @description:
 * @version：v1.0
 * @date: 2024/6/1 21:33
 */
@Slf4j
@RestController
@RequestMapping("/company")
@RequiredArgsConstructor
public class CompanyController extends BaseController {

    private final CompanyService companyService;
    private final CompanyConverter companyConverter;
    private final SuperInterface<SysCompany> superInterface;



    /**
     * 新增/修改
     * @param dto 新增对象
     * @apiNote 通过是否存在主键,判定是新增还是修改;id为空则为新增，反之;
     * @author lslands
     * @date 2024/5/18 23:06
     * @return R<void>
     */
    @PostMapping("/saveOrUpdate")
    public R save(@RequestBody CompanyFormDto dto) throws BaseException {
        SysCompany record = companyConverter.formEntity(dto);
        return Objects.isNull(dto.getId())
                ? toCreateAjax(superInterface.handInsert(record))
                : toUpdateAjax(superInterface.handUpdateById(record));
    }

    /**
     * 更新状态
     * @param record 更新状态对象
     * @return R<void>
     */
    @PatchMapping("/updateStatus")
    public R updateStatus(@RequestBody BaseEnabled record) throws BaseException {
        return toUpdateAjax(
                superInterface.handEnabledById(record.getId(), record.getEnabled(), record.getRevision()));
    }

    /**
     * 企业添加用户
     * @param dto 企业添加用户对象
     * @return R<void>
     */
    @PatchMapping("/assignUser")
    public R<?> assignUser(@RequestBody AssignUserDto dto){
        //todo 待开发
        return R.success();
    }

    /**
     * 单条数据查询
     * @param id 主键ID|1
     * @author lslands
     * @date 2024/5/19 23:05
     * @return R<SysCompany>
     */
    @GetMapping("/getById")
    public R<SysCompany> getById(@RequestParam Long id){
        return R.success(companyService.getById(id));
    }

    /**
     * 分页模糊查询列表
     * @param search 企业模糊查询对象
     * @return R<Page<SysCompany>>
     */
    @GetMapping("/page")
    public R<Page<SysCompany>> page(@ModelAttribute CompanySearch search) throws BaseException {
        BaseQuery query = companyConverter.queryEntity(search);
        SysCompany build = SysCompany.builder()
                .name(search.getName()).parentId(search.getCompanyId())
                .build();
        build.setEnabled(search.getEnabled());
        return R.success(superInterface.getPage(query, build));
    }

    /**
     * 查询企业树
     * @return 企业树
     */
    @GetMapping("/treeOptions")
    public R<List<Options<Integer>>> companyTree() throws BaseException {
        return R.success(companyService.listOptions(JwtTokenAnalysisUtli.companyId()));
    }


    /**
     * 单条逻辑删除
     * @param id 主键ID|1
     * @author lslands
     * @date 2024/5/19 22:28
     */
    @DeleteMapping("/deleteOne")
    public R deleteOne(@RequestBody Long id) throws BaseException {
        return toDeleteAjax(superInterface.deleteOne(id));
    }

    /**
     * 批量删除
     * @param ids 主键ID集|[1,2]
     * @param delete 是否物理删除标记|false
     * @author lslands
     * @date 2024/5/19 22:29
     */
    @DeleteMapping("/deleteBatch/{delete}")
    public R deleteBatch(@RequestBody List<Long> ids,@PathVariable("delete") Boolean delete) throws BaseException {
        return toDeleteAjax(superInterface.deleteBatch(delete,ids));
    }

}
